package com.stox.client.ui.javafx.chartingy.widget.toolbar;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;

import com.stox.client.ui.javafx.chartingy.event.listener.HorizontalLineMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.HorizontalRayMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.HorizontalSegmentMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.TrendLineMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.TrendRayMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.TrendSegmentMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.VerticalLineMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.VerticalRayMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.event.listener.VerticalSegmentMouseEventStrategy;
import com.stox.client.ui.javafx.chartingy.model.chart.Chart;
import com.stox.client.ui.javafx.chartingy.view.ChartingView;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.core.i18n.MessageSource;

public class DrawingToolBar extends ToolBar {

	private final ChartingView chartingView;

	public DrawingToolBar(final ChartingView chartingView) {
		this.chartingView = chartingView;
		UI.style(this, CSS.glass, CSS.drawingToolBar);
		setOrientation(Orientation.VERTICAL);
		getItems().addAll(createClearAllButton(), createHorizontalLineButton(), createHorizontalRayButton(), createHorizontalSegmentButton(), createVerticalLineButton(), createVerticalRayButton(), createVerticalSegmentButton(), createTrendLineButton(), createTrendRayButton(), createTrendSegmentButton(), UI.spacer());
	}

	private Node createClearAllButton() {
		final Button clearAllButton = UI.mediumCrossButton(MessageSource.get("ClearAllDrawings"));
		clearAllButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(final ActionEvent event) {
				chartingView.getPrimaryChart().removeAllShapes();
				for (final Chart<?, ?> chart : chartingView.getCharts()) {
					chart.removeAllShapes();
				}
			}
		});
		return clearAllButton;
	}


	private Node createHorizontalLineButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.horizontalLineButton(), new HorizontalLineMouseEventStrategy());
	}

	private Node createHorizontalRayButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.horizontalRayButton(), new HorizontalRayMouseEventStrategy());
	}

	private Node createHorizontalSegmentButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.horizontalSegmentButton(), new HorizontalSegmentMouseEventStrategy());
	}

	private Node createVerticalLineButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.verticalLineButton(), new VerticalLineMouseEventStrategy());
	}

	private Node createVerticalRayButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.verticalRayButton(), new VerticalRayMouseEventStrategy());
	}

	private Node createVerticalSegmentButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.verticalSegmentButton(), new VerticalSegmentMouseEventStrategy());
	}

	private Node createTrendSegmentButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.trendSegmentButton(), new TrendSegmentMouseEventStrategy());
	}

	private Node createTrendRayButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.trendRayButton(), new TrendRayMouseEventStrategy());
	}

	private Node createTrendLineButton() {
		return chartingView.prepareMouseEventStrategyToggleButton(UI.trendLineButton(), new TrendLineMouseEventStrategy());
	}

}
